﻿package net.hainan.chat;


import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.packet.Presence;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.scenery.R;
/****
 * 登录openfire服务器
 * */
public class Login extends Activity implements OnClickListener
{
	private final String ACCOUNT_KEY = "login_account";
	private final String PASSWORD_KEY = "login_password";

	private EditText metAccount;
	private EditText metPassword;

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);
		Button btnLogin = (Button) findViewById(R.id.btnLogin);
		metAccount = (EditText) findViewById(R.id.etAccount);
		metPassword = (EditText) findViewById(R.id.etPassword);
		btnLogin.setOnClickListener(this);
		Openfire.oUtil = new Util(this);


		metAccount.setText(Openfire.oUtil.getString(ACCOUNT_KEY));
		metPassword.setText(Openfire.oUtil.getString(PASSWORD_KEY));
		setTitle(R.string.app_login_name);
	}

	public void onClick(View view)
	{
		
		try{
			ConnectionConfiguration connConfig = new ConnectionConfiguration(
					 "192.168.0.101", 5222, "");

			Openfire.oConnection = new XMPPConnection(connConfig);

			Openfire.oConnection.connect();
			String account = metAccount.getText().toString();
			String password = metPassword.getText().toString();
			Openfire.oConnection.login(account, password);
			Openfire.oCurrentAccount = account;
			Presence presence = new Presence(Presence.Type.available);
			Openfire.oConnection.sendPacket(presence);
			Openfire.oUtil.saveString(ACCOUNT_KEY, account);
			Openfire.oUtil.saveString(PASSWORD_KEY, password);
			Intent intent = new Intent(this, LvyouActivity.class);
			startActivity(intent);
			finish();
		}
		catch (Exception e)
		{
			Openfire.oUtil.showMsg("连接错误.");
		}
		
	}

}